#include "DeviceSelectEvent.h"
#include "logo_dat.h" 
#include "../aw_gui_app.h"
#include "filenav.h"
#include "ctrlconvert.h"
#include "NetmenuSetupEvent.h"
#ifdef ENABLE_NETWORK
extern u8_t net_entrance;
#endif

/***********************************************************/
#define DEVDRAWNUM 4 // how many device should be draw
_DEV_LIST_ DevList; //Mounted Device List
_DEV_ITEM_ DevItem; //data buff for device info when the device mount
_DEV_NODE_ *pLeftHeadDevNode = NULL; //the Lefthead device Node of the 4 devices Node on device select window, use it to locate another 3 device node
_DEV_NODE_ *pCurrentCursorNode = NULL; //Current Focus dev Node on show
unsigned char DevMountNum = 0; // the Number of Mounted Dev  ++ or -- in aw_system_app.c
unsigned char CurrentCursor = 0; //the current drawinfo number of the 4 icons on device select window
char CurrentDevPath[12] = {0}; //the current Device path eg:"/mnt/c"
char CurrentDrawDeviceName[20] = {0}; // DrawDeviceNameStr

extern INT32U NODIR_FLAG;
extern int SHOW_WINDOW_NUM;
extern INT8U CurrentOsdSize;
/*_DEV_NODE_ NodeCalendar = {
	{{{0,0,0,0,(INT32U)ICON_CALENDER_LOSE,0,0},{0,0,0,0,CALENDAR_STR,0,0}},"Calendar","/mnt/A",1,1,STORAGE_ICON_CALENDAR,DevicePro},
	&NodeSetup,
	NULL

};*/

/*char setupstring[20] = "setup";
void ChangeStrType()
{
	char *p_str,*str_get;
	INT16U StrLen;
	 str_get = AVMem_umalloc(60) ;
	 p_str = (char *)AWGetResource(SETUP_STR, &StrLen);
        StrLen = (StrLen < 50)?StrLen:50 ;
        strncpy(str_get, p_str, StrLen);
        str_get[StrLen] = 0 ;
	 sprintf(setupstring,"%s",str_get) ;
	 AVMem_free(str_get);

//	 strncpy(NodeSetup->DevItem->DevName, setupstring, sizeof(NodeSetup->DevItem->DevName));

}*/

		
_DEV_NODE_ NodeSetup = {
	{{{0,0,0,0,(INT32U)ICON_SETUP_LOSE_480,0,0},{0,0,0,0,SETUP_STR,0,0}},"SETUP","/mnt/B","NO NAME",1,1,STORAGE_ICON_SETUP,DevicePro},
	NULL,
	NULL
};
//at least, just can show the device name string, 
//but can't show the unique device name when more same device mounted. just like "USB-C","USB-D","USB-E"....
//if wana do this , need to make some change for device string array.
INT32U DevNameStr[] = {
	SD_STR,
	MS_STR,
	CF_STR,
	XD_STR,
	SD_STR,
	USB_STR,
	#ifdef ENABLE_NETWORK
	UPNP_STR,
	YAHOO_NEW_STR,
	GOOGLE_NEW_STR,
	FRAMECHANNEL_STR,
#endif
};

extern INT8U FileListOnShowFlag;
extern INT32U gGetFileType ;
char CurDriver[10] = "/mnt/C/";
extern void InitPreviewList(void);
extern unsigned char osdsettpval(unsigned char num,unsigned char level);

//Initial Mounted Device List, calendar is the head node, setup is the second node.
void InitialDevList(_DEV_LIST_ *pDevList)
{
	NodeSetup.DevItem.DevDrawInfo.DevIcon.draw_data = (INT32U)ICON_SETUP_LOSE;
	pDevList->Head = &NodeSetup;
	pDevList->End = &NodeSetup;
	pDevList->count = 1;
	DevMountNum = pDevList->count;
}
#ifdef ENABLE_NETWORK
//check if current dev is network dev
unsigned char IsNetWorkDev(void)
{
	switch (pCurrentCursorNode->DevItem.DevNameId)
	{
		case STORAGE_ICON_UPNP:
		case STORAGE_ICON_YAHOO:
		case STORAGE_ICON_GOOGLE:
		case STORAGE_ICON_FRAMECHANNEL:
			return 1;
		default:
			return 0;
	}
}
#endif

unsigned char CheckDevName(char *NameStr)
{
	if (!NameStr)
		return 0xFF;
	if(strcmp((char*)NameStr,"NAND") == 0)
	{
		return STORAGE_ICON_NAND;
	}
	if(strcmp((char*)NameStr,"CF") == 0)
	{
		 return  STORAGE_ICON_CF;
	}
	else if((strcmp(NameStr,"SD") == 0)||(strcmp(NameStr,"MMC") == 0))
	{
		return  STORAGE_ICON_SD;
	}
	else if(strcmp(NameStr,"XD") == 0)
	{
		return  STORAGE_ICON_XD;
	}
	else if(strcmp(NameStr,"MS") == 0)
	{
		return  STORAGE_ICON_MS;
	}
	else if((strcmp(NameStr,"UDISK") == 0)||(strcmp(NameStr,"PTP") == 0)
#ifdef ENABLE_BLUETOOTH_OPP
	||(strcmp(NameStr,"BTOPP") == 0)
#endif
	)
	{
		return  STORAGE_ICON_USB;
	}
	else if(strcmp((char*)NameStr,"Setup") == 0)
	{
		return STORAGE_ICON_SETUP;
	}
	#ifdef ENABLE_NETWORK
	else if(strcmp((char*)NameStr,"UPNP") == 0)
	{
		return STORAGE_ICON_UPNP;
	}
	else if(strcmp((char*)NameStr,"FLI_GALLERY") == 0)
	{
		return STORAGE_ICON_YAHOO;
	}
	else if(strcmp((char*)NameStr,"PIC_GALLERY") == 0)
	{
		return STORAGE_ICON_GOOGLE;
	}
	else if(strcmp((char*)NameStr,"FRAMECHANNEL") == 0)
	{
	        return STORAGE_ICON_FRAMECHANNEL;
	}        
#ifdef BROWSER_SUPPORT
	else if(strcmp((char*)NameStr,"BROWSER") == 0)
	{
	        return STORAGE_ICON_BROWSER;
	}        
#endif
#ifdef RSS_GALLERY_SUPPORT
	else if(strcmp((char*)NameStr,"RSS_GALLERY") == 0)
	{
		return STORAGE_ICON_RSSGALLERY;
	}
#endif
	#endif

	return 0xFF;
}

INT32U GetDevIconData(unsigned char number,unsigned char selcet)
{	
	switch(number)
	{
		case STORAGE_ICON_NAND:
			if(selcet){
					return (INT32U)ICON_CF_FOCUS;
				}
			else{
					return (INT32U)ICON_CF_LOSE;
				}
			break;	


		case STORAGE_ICON_CF:
			if(selcet){
					return (INT32U)ICON_CF_FOCUS;
				}
			else{
					return (INT32U)ICON_CF_LOSE;
				}
			break;	
		case STORAGE_ICON_SD:
			if(selcet){
					return (INT32U)ICON_SD_FOCUS;
				}
			else{
					return (INT32U)ICON_SD_LOSE;
				}
			break;
		case STORAGE_ICON_XD:
			if(selcet){
					return (INT32U)ICON_XD_FOCUS;
				}
			else{
					return (INT32U)ICON_XD_LOSE;
				}
			break;
		case STORAGE_ICON_MS:
			if(selcet){
					return  (INT32U)ICON_MS_FOCUS;
				}
			else{
					return (INT32U)ICON_MS_LOSE;
				}
			break;
		case STORAGE_ICON_USB:
			if(selcet){
					return (INT32U)ICON_USB_FOCUS;
				}
			else{
					return (INT32U)ICON_USB_LOSE;
				}
			break;
		case STORAGE_ICON_SETUP:
			if(selcet){
					return (INT32U)ICON_SETUP_FOCUS;
				}
			else{
					return (INT32U)ICON_SETUP_LOSE;
				}
			break;
				#ifdef ENABLE_NETWORK		
		case STORAGE_ICON_UPNP:
			if(selcet)
				return (INT32U)ICON_UPNP_SELECT;
			else
				return (INT32U)ICON_UPNP_LOSE;
			break;
		case STORAGE_ICON_YAHOO:
			if(selcet)
				return (INT32U)ICON_YAHOO_SELECT;
			else
				return (INT32U)ICON_YAHOO_LOSE;
			break;
		case STORAGE_ICON_GOOGLE:
			if(selcet)
				return (INT32U)ICON_GOOGLE_SELECT;
			else
				return (INT32U)ICON_GOOGLE_LOSE;
			break;
		case STORAGE_ICON_FRAMECHANNEL:	
			if(selcet)
				return (INT32U)ICON_ASS_SELECT;
			else
				return (INT32U)ICON_ASS_LOSE;
			break;
#ifdef BROWSER_SUPPORT
		case STORAGE_ICON_BROWSER:	
			if(selcet)
				return (INT32U)ICON_IGOOGLE_SELECT;
			else
				return (INT32U)ICON_IGOOGLE_LOSE;
			break;
#endif			
#ifdef RSS_GALLERY_SUPPORT
        case STORAGE_ICON_RSSGALLERY:
			if(selcet)
				return (INT32U)ICON_UPNP_SELECT;
			else
				return (INT32U)ICON_UPNP_LOSE;
			break;
#endif		
		#endif

		default:
		break;
	}
}

INT32U GetDevNameData(unsigned char number)
{
	return DevNameStr[number];
}

unsigned char ResourceDevNameCat(char *Dest_Str, INT32U Src_ResStr, char *Src_Str)
{
	char *p_str = NULL;
	unsigned short StrLen;
	p_str = (char *)AWGetResource(Src_ResStr, &StrLen);

	if ((!Dest_Str) || (!Src_Str) || (!p_str))
		return 0;

	strncpy(Dest_Str, p_str, StrLen);
	Dest_Str[StrLen] = '\0' ;

	if(Src_ResStr != UPNP_STR)
		return 2;
	
	if (strncmp("NO NAME", Src_Str, 7))
	{
		strcat(Dest_Str, "-");
		strcat(Dest_Str, Src_Str);
		Dest_Str[10] = '\0';
	}

	return 1;
}

void RefreshCurrDrawDevName(char *Dest_Str, INT32U Src_ResStr, char *Src_Str)
{
	ResourceDevNameCat(Dest_Str, Src_ResStr, Src_Str);
}
//enter key action of all device
void DevicePro(unsigned char DevNameId)
{
	switch (DevNameId)
	{
		case STORAGE_ICON_SETUP:
			GUIHideWindow(WinDeviceSelect);
			GUIShowWindow(WinSetup_net_menu);	
			break;
		#ifdef ENABLE_NETWORK			
		case STORAGE_ICON_YAHOO:
		case STORAGE_ICON_GOOGLE:
		case STORAGE_ICON_FRAMECHANNEL:
        #ifdef RSS_GALLERY_SUPPORT
		case STORAGE_ICON_RSSGALLERY:
        #endif		    
			#if 1
 			if(!FileNavCD(CurDriver))
			{
	 			InitPreviewList();
				Init_FileList_Prepare_List(MEDIA_PIC,SHOW_WINDOW_NUM);
				GUIShowWindow("Win15Pic_net");
 			}
			break;
			#endif
		case STORAGE_ICON_UPNP:
			gGetFileType = AW_PIC_LIST|AW_AUDIO_LIST|AW_VIDEO_LIST|MEDIA_TXT|NAV_IS_DIR;	
			FileListOnShowFlag=0;
			NODIR_FLAG = 0;
			GUIHideWindow("WinDeviceSelect");
			//ReloadLogo(Logo_File_List, sizeof(Logo_File_List));
			GUIShowWindow("WinFileList");
			break;
        #ifdef BROWSER_SUPPORT
		case STORAGE_ICON_BROWSER:
            AWBrowserKeyDo(UOP_END_PAUSE,0);
			GUIHideWindow("WinDeviceSelect");
    		#ifdef ENABLE_CURSOR
   	        HideCursor(); /* change cursor status as off so that next ShowCursor can enable OSD */
	    	ShowCursor(400,200);
		    #endif
			break;
        #endif			
		#endif

		case STORAGE_ICON_NAND:
		case STORAGE_ICON_CF:
		case STORAGE_ICON_SD:
		case STORAGE_ICON_XD:
		case STORAGE_ICON_MS:
		case STORAGE_ICON_USB:
			strcpy(CurrentDevPath, pCurrentCursorNode->DevItem.DevPointName);
			strcat(CurrentDevPath,"/");
			strcpy(CurDriver, CurrentDevPath);			
			gGetFileType = AW_PIC_LIST|AW_AUDIO_LIST|AW_VIDEO_LIST|MEDIA_TXT|NAV_IS_DIR;	
			FileListOnShowFlag=0;
			GUIHideWindow(WinDeviceSelect);			
			GUIShowWindow(WinFileTypeSelect);
		default:
			break;
	}
}

//capture the device info when mount a new device
void GetAddDevNodeItem(_DEV_ITEM_ *DevItem, DEV_MOUNT_INFO* devmountmsg, unsigned char DevMountNum)
{
	char *NameStr;
	NameStr = devmountmsg->mount_device_type;
	DevItem->DevNameId = CheckDevName(NameStr);
	DevItem->DevDrawInfo.DevIcon.draw_data = GetDevIconData(DevItem->DevNameId, 0);
	DevItem->DevDrawInfo.DevName.draw_data = GetDevNameData(DevItem->DevNameId);
	strcpy (DevItem->DevName, (const char*)NameStr);
	
//	NameStr = devmountmsg->mount_device_label;
//	memcpy(DevItem->DevLable, NameStr, 9);
//	DevItem->DevLable[9] = '\0';

	NameStr = devmountmsg->mount_point_name;
	strcpy(DevItem->DevPointName, (const char*)NameStr);
	DevItem->DevId = DevMountNum;
	DevItem->Proc = DevicePro;
}
// add the new mounted device to the end of the device list
unsigned char AddDevNode(_DEV_ITEM_ DevItem, _DEV_LIST_ *pDevList)
{
	_DEV_NODE_ *pNewNode;
	_DEV_NODE_ *pNode;

	pNode = pDevList->End;
	
	pNewNode = (_DEV_NODE_ *)AVMem_malloc(sizeof(_DEV_NODE_));

	if (pNewNode == NULL)
		return 0;
	memcpy(&(pNewNode->DevItem), &(DevItem), sizeof(_DEV_ITEM_));
	pNewNode->Next = NULL;
	pNewNode->Pre = NULL;
	
	if (pNode)
	{
		pNode->Next = pNewNode;
		pNewNode->Pre = pNode;
	}
	else
	{
		pNode = pNewNode;
	}
	pDevList->End = pNewNode;
	pDevList->count++;
	pLeftHeadDevNode = pDevList->End; //set the Lefthead device Node as the end
	return 1;
}

//remove it from the device list and release its memory when a device unmount
unsigned char RemoveDevNode(DEV_MOUNT_INFO* devmountmsg, _DEV_LIST_ *pDevList, unsigned char DevMountNum)
{
	_DEV_NODE_ *pNode;
	_DEV_NODE_ *pNodeSav;

	// search the unmount device from head to end by it's point name as "/mnt/c"
	pNode = pDevList->Head;
	while(strcmp((char*)(devmountmsg->mount_point_name), pNode->DevItem.DevPointName) != 0)
	{
		pNode = pNode->Next;
		if(pNode==NULL)
			return 0;
	}
	
	pNodeSav = pNode;

	if (DevMountNum == 0) //if it's the last device
	{
		pDevList->Head = NULL;
		pDevList->End = NULL;
		pDevList->count = 0;
		pLeftHeadDevNode = NULL;
		pCurrentCursorNode = NULL;
		CurrentCursor = 0;
		AVMem_free(pNode);
		return 1;
	}
	else
	{
		if (pNode == pDevList->Head) //if it's the head
		{
			pDevList->Head= pNode->Next;
			pNode->Next->Pre  = NULL;

			pNodeSav = pNode->Next;
			while (pNodeSav)
			{
				pNodeSav->DevItem.DevId--;
				pNodeSav = pNodeSav->Next;
			}
		}
		else if (pNode == pDevList->End)//if it's the end
		{
			pDevList->End= pNode->Pre;
			pNode->Pre->Next = NULL;
		}
		else
		{
			pNode->Pre->Next = pNode->Next;
			pNode->Next ->Pre = pNode->Pre;
			pNodeSav = pNode->Next;
			while (pNodeSav)
			{
				pNodeSav->DevItem.DevId--;
				pNodeSav = pNodeSav->Next;
			}
		}
		//if it's the current device
		if (pCurrentCursorNode == pNode)
		{
			pCurrentCursorNode = NULL;
		}
        if(CurrentCursor)
            CurrentCursor--;
	}
	pDevList->count--;
	pLeftHeadDevNode = pDevList->End; //set the Lefthead device Node as the end  
//	CurrentCursor = 0; // set the lefthead device is the current device
	AVMem_free(pNode);
	return 1;
}

unsigned char FindDevNodeByDevId(_DEV_LIST_ *pList, _DEV_NODE_ **pNode, unsigned char id)
{
	pNode = &(pList->Head);
	while (*pNode)
	{
		if ((*pNode)->DevItem.DevId == id)
			return 1;
		*pNode = (*pNode)->Next;
	}
	return 0;
}

//according the Lefthead node and the Current Num to refresh the 4 device icon datainfo
void RefreshDevDrawInfoData(PHWND hwnd, _DEV_NODE_ *pLeftDevNode, unsigned char CursorNum)
{
	INT8U offset;
	unsigned char i = 0;
	_DEV_NODE_ *pDevNodeTemp;
	INT32S word_width = 120,font_height, word_len, str_len ;
	static char sDevLable[12];
#ifdef TV_MODE_1080P	
	if(CurrentOsdSize==OsdSize1080p)
		offset = 80;
	else
#endif
#ifdef TV_MODE_720P	
	if(CurrentOsdSize==OsdSize720p)
		offset = 54;
	else
#endif
		offset = 34;

	if ((pLeftDevNode == NULL) || (hwnd == NULL) || (CursorNum>DEVDRAWNUM-1))
	{
		return;
	}

	pDevNodeTemp = pLeftDevNode;

	//refresh left arrow icon
	if (pDevNodeTemp->Next)
	{
			hwnd->draw_info[6].draw_data = (INT32U) ICON_LEFT_ARROW;
	}
	else
	{
			hwnd->draw_info[6].draw_data = (INT32U) ICON_ARROW_EMPTY;
	}

	//refresh device icon and its name icon
	while((pDevNodeTemp) && (i<DEVDRAWNUM))
	{
		if (i==CursorNum)
		{
			pDevNodeTemp->DevItem.DevDrawInfo.DevIcon.draw_data = GetDevIconData(pDevNodeTemp->DevItem.DevNameId, 1);
	#ifdef ENABLE_NETWORK
                #ifdef BROWSER_SUPPORT
            if (pDevNodeTemp->DevItem.DevNameId == STORAGE_ICON_BROWSER)
            {
			    strncpy(sDevLable, pDevNodeTemp->DevItem.DevLable,12); 
	            str_len = AWGetTextSize(sDevLable, 0,&word_width,&font_height,&word_len, AWTF_ASCII) ;
				sDevLable[str_len]=0;
				hwnd->draw_info[5].draw_data = (INT32U)sDevLable; 
            }else                    
                #endif			    

			if (pDevNodeTemp->DevItem.DevNameId != STORAGE_ICON_UPNP)
			{
				RefreshCurrDrawDevName(CurrentDrawDeviceName, pDevNodeTemp->DevItem.DevDrawInfo.DevName.draw_data, pDevNodeTemp->DevItem.DevLable);
				hwnd->draw_info[5].draw_data = (INT32U) CurrentDrawDeviceName;
			}
			else
			{
				strncpy(sDevLable, pDevNodeTemp->DevItem.DevLable,12); 
				str_len = AWGetTextSize(sDevLable, 0,&word_width,&font_height,&word_len, AWTF_ASCII) ;
				sDevLable[str_len]='~';
				sDevLable[str_len+1]=0;  
				hwnd->draw_info[5].draw_data = (INT32U)sDevLable; 
			}
			hwnd->draw_info[5].draw_type = DT_STRING_DATA|DT_STRING_DATA_CENTER;
			#else
			hwnd->draw_info[5].draw_data = pDevNodeTemp->DevItem.DevDrawInfo.DevName.draw_data;
			#endif

			hwnd->draw_info[5].draw_data = pDevNodeTemp->DevItem.DevDrawInfo.DevName.draw_data;
//			hwnd->draw_info[5].draw_data = (INT32U)  pDevNodeTemp->DevItem.DevName;
//			hwnd->draw_info[5].draw_type = DT_STRING_DATA;
			hwnd->draw_info[4].draw_pos_x = hwnd->draw_info[i].draw_pos_x;
			hwnd->draw_info[5].draw_pos_x = hwnd->draw_info[i].draw_pos_x + offset;
			pCurrentCursorNode = pDevNodeTemp;
		}
		else
		{
			pDevNodeTemp->DevItem.DevDrawInfo.DevIcon.draw_data = GetDevIconData(pDevNodeTemp->DevItem.DevNameId, 0);
		}
		hwnd->draw_info[i].draw_data = (INT32U) pDevNodeTemp->DevItem.DevDrawInfo.DevIcon.draw_data;	
		pDevNodeTemp = pDevNodeTemp->Pre;
		i++;	
	}
	//refresh right arrow icon
	if ((i==DEVDRAWNUM) && pDevNodeTemp)
	{	
			hwnd->draw_info[7].draw_data = (INT32U) ICON_RIGHT_ARROW;
	}
	else
	{
			hwnd->draw_info[7].draw_data = (INT32U) ICON_ARROW_EMPTY;
		//clean the rest icons
		for (;i<DEVDRAWNUM;i++)
				hwnd->draw_info[i].draw_data = (INT32U) ICON_DEVICEEMPTY;	
	}
}

//Refresh the DeviceSelectWindow, flag 1:first into device select window, 0:refresh the current device select window
void RefreshDevWindow(PHWND hwnd, unsigned char CursorNum, unsigned char flag)
{
	unsigned char i=0;
	unsigned int bk_color;
	
	if (!hwnd)
		return;
	RefreshDevDrawInfoData(hwnd, pLeftHeadDevNode, CursorNum);

	bk_color = AWSetForeground(0);
#ifdef TV_MODE_1080P	
	if(CurrentOsdSize==OsdSize1080p){
		AWFillRectTxt(hwnd->draw_info[0].draw_pos_x, hwnd->draw_info[4].draw_pos_y,900, 40);
	}
	else
#endif
#ifdef TV_MODE_720P
	if(CurrentOsdSize==OsdSize720p){
		AWFillRectTxt(hwnd->draw_info[0].draw_pos_x, hwnd->draw_info[4].draw_pos_y,900, 36);
	}
	else
#endif
	{
		AWFillRectTxt(hwnd->draw_info[0].draw_pos_x, hwnd->draw_info[4].draw_pos_y,480, 30);
	}
	AWSetForeground(bk_color);

	if (flag)
	{
		AWShowWinControl(hwnd, 0);
		AWSetFocus(hwnd);
	}
	else
	{
		for(i=0;i<hwnd->draw_info_num;i++)
		AWSendMessage(hwnd, WM_PAINT_DRAW_INFO, i);
    		AWSendMessage(hwnd,WM_PAINT_FINISH,0);
	}
	
}

//according the Lefthead node and the Coursor number (0-3) to locate the Current Device Node
void FindCursorDevNode(_DEV_NODE_ *pLeftDevNode, _DEV_NODE_ **CursorDevNode, unsigned char CursorNum)
{
	unsigned char i = 0;
	_DEV_NODE_ *pDevNodeTemp = NULL;

	if ((!pLeftDevNode) || (CursorNum>DEVDRAWNUM-1))
		return;
	
	pDevNodeTemp = pLeftDevNode;

	while((pDevNodeTemp)&&(i<CursorNum))
	{
		if (pDevNodeTemp->Pre == NULL)
		{
			CurrentCursor = i;
			(*CursorDevNode) = pDevNodeTemp;
			return;
		}
		pDevNodeTemp = pDevNodeTemp->Pre;
		i++;
	}
	*(CursorDevNode) = pDevNodeTemp;
}

//action function of left or right keypress, to shift Current device node
//return 1 shift success, 0 shift faild
unsigned char ShiftDevInWin(INT32U key_value)
{
	switch (key_value)
	{
		case UOP_LEFT:
			if ((--CurrentCursor) == 0xFF)
			{
				CurrentCursor = 0;			
				if (pLeftHeadDevNode->Next)
				{
					pLeftHeadDevNode = pLeftHeadDevNode->Next;
					pCurrentCursorNode = pLeftHeadDevNode;
				}
				else
				{
					#ifdef WISTAR_VERSION
					pCurrentCursorNode = DevList.Head;
					pLeftHeadDevNode =  DevList.Head;
					//CurrentCursor = DevMountNum -1 ;
					FindCursorDevNode(pLeftHeadDevNode, &pCurrentCursorNode, CurrentCursor);
					#else
					return 0;
					#endif
				}
			}
			else
			{
				FindCursorDevNode(pLeftHeadDevNode, &pCurrentCursorNode, CurrentCursor);				
			}
			break;
		case UOP_RIGHT:
		#ifdef WISTAR_VERSION
			if (!((++CurrentCursor) %DEVDRAWNUM))
		#else
			if ((++CurrentCursor) == DEVDRAWNUM)
		#endif
			{
				if (pCurrentCursorNode->Pre)
				{
					pCurrentCursorNode = pCurrentCursorNode->Pre;
					pLeftHeadDevNode = pCurrentCursorNode;
					CurrentCursor = 0;
				}
				else
				{
				#ifdef WISTAR_VERSION
					pCurrentCursorNode = DevList.End;
					pLeftHeadDevNode = DevList.End;
					CurrentCursor = 0;	
				#else
					CurrentCursor--;	
					return 0;
				#endif
				}
			}
			else
			{
				if (pCurrentCursorNode == DevList.Head)
				{
					#ifdef WISTAR_VERSION
					CurrentCursor = 0;
					pCurrentCursorNode = DevList.End;
					pLeftHeadDevNode = DevList.End;
					FindCursorDevNode(pLeftHeadDevNode, &pCurrentCursorNode, CurrentCursor);				
					#else
					CurrentCursor--;
					return 0;
					#endif
				}
				FindCursorDevNode(pLeftHeadDevNode, &pCurrentCursorNode, CurrentCursor);				
			}
			break;
		default:
			break;
	}
	return 1;
}
#ifdef ENABLE_NETWORK		
extern INT8U network_connected_flag;
#endif
INT32S WinDeviceSelectInfoOnPaint(PHWND hwnd, INT32S lpParam)
{
#ifdef ENABLE_NETWORK
	if(network_connected_flag)
	{	
	    RefreshDevDrawInfoData(hwnd, pLeftHeadDevNode, CurrentCursor);
		hwnd->pClass->ClassProc(hwnd, WM_PAINT, lpParam) ;
		AWRLEBitmap(750,430,(INT8U *)ICON_NET);	
		return 1;
	}
	else
	{ 
        RefreshDevWindow(hwnd, CurrentCursor, 0);
        AWSetDrawRectFlag(1);
        AWClearRect(750,430,50,50);
        AWSetDrawRectFlag(0);
        return 1;
    }  
#else
    #if (defined LCD_800X480)||(defined LCD_800X600)  
        RefreshDevWindow(hwnd, CurrentCursor, 0);
    	return 1;
	#else
		return 0;   
	#endif
#endif
}


INT32S WinDeviceSelectInfoOnShow(PHWND hwnd, INT32S lpParam)
{
	pLeftHeadDevNode = DevList.End;
//	CurrentCursor = 0;
	pCurrentCursorNode = DevList.End;
	RefreshDevWindow(hwnd, CurrentCursor, 1);
	return 0;
}
INT32S WinDeviceSelectInfoOnKeyPress(PHWND hwnd, INT32S lpParam)
{
	INT32U *key_value = (INT32U *)lpParam ;
	switch(*key_value)
	{
		case UOP_LEFT:
		case UOP_RIGHT:
			if (ShiftDevInWin(*key_value))
				RefreshDevWindow(hwnd, CurrentCursor, 0);
			break;
		case UOP_ENTER:
			pCurrentCursorNode->DevItem.Proc(pCurrentCursorNode->DevItem.DevNameId);
			break;
		default:
			break;
	}
	*key_value = UOP_NOP;
	return 0 ;
}

INT32S WinDeviceSelectOnShow(PHWND hwnd, INT32S lpParam)
{ 
#ifdef TV_MODE_1080P
	if(CurrentOsdSize == OsdSize1080p)
		osdsettpval(5,0xf);
	else
#endif
#ifdef ENABLE_NETWORK
	net_entrance = FALSE;
#endif
	osdsettpval(1,0xf);
	return 0 ;
}
/***********************************************************/
